package com.kdd.campus.sh.controller;

import com.kdd.campus.common.entity.CommonResult;
import com.kdd.campus.common.util.RedisUtil;
import com.kdd.campus.sh.po.ShAddress;
import com.kdd.campus.sh.service.AddressService;
import com.kdd.campus.sh.util.JwtTokenUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/****
 * @Author:fcp
 * @Description:
 * @Date 2019/6/14 0:18
 *****/
@Api(value = "地址")
@RestController
@RequestMapping("/address")
public class ShAddressController extends BaseController{

    @Autowired
    private AddressService addressService;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @ApiOperation(value = "获取用户的地址")
    @GetMapping
    public CommonResult<List<ShAddress>> getUserAddress(){
        // 根据token，获取Redis中的userId
        Long userId = Integer.toUnsignedLong((Integer) RedisUtil.get(jwtTokenUtil.getToken(request)));
        List<ShAddress> addressList = addressService.findByUserId(userId);
        return CommonResult.success(addressList);
    }

    @ApiOperation(value = "根据id获取详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "addressId", value = "addressId", required = true, dataType = "Integer")}
    )
    @GetMapping(value = "/{addressId}" )
    public CommonResult getAddressById(@PathVariable @NotNull Integer addressId){
        // 根据token，获取Redis中的userId
        Long userId = Integer.toUnsignedLong((Integer) RedisUtil.get(jwtTokenUtil.getToken(request)));
        ShAddress address = addressService.findAddressById(userId,addressId);
        return CommonResult.success(address);
    }

    @ApiOperation(value = "获取用户的默认地址")
    @GetMapping("/default")
    public CommonResult<ShAddress> getDefaultAddress(){
        // 根据token，获取Redis中的userId
        Long userId = Integer.toUnsignedLong((Integer) RedisUtil.get(jwtTokenUtil.getToken(request)));
        ShAddress address = addressService.findDefaultAddress(userId);
        return CommonResult.success(address);
    }

    @ApiOperation(value = "修改地址",notes = "根据地址ID和用户ID修改ShAddress信息")
    @PutMapping
    public CommonResult update(@RequestBody @ApiParam(name = "ShAddress对象",value = "传入JSON数据") ShAddress address){
        if (address.getId()==null){
            return CommonResult.failed("地址id为null");
        }
        // 根据token，获取Redis中的userId
        Long userId = Integer.toUnsignedLong((Integer) RedisUtil.get(jwtTokenUtil.getToken(request)));
        address.setUserId(userId);
        addressService.update(address);
        return CommonResult.success();
    }

    /***
     * 新增ShAddress数据
     * @param address
     * @return
     */
    @ApiOperation(value = "ShAddress添加",notes = "添加ShAddress方法详情")
    @PostMapping
    public CommonResult add(@RequestBody  @ApiParam(name = "ShAddress对象",value = "传入JSON数据",required = true) ShAddress address){
        // 根据token，获取Redis中的userId
        Long userId = Integer.toUnsignedLong((Integer) RedisUtil.get(jwtTokenUtil.getToken(request)));
        address.setUserId(userId);
        addressService.add(address);
        return CommonResult.success();
    }


    /***
     * 根据ID删除品牌数据
     * @param addressId
     * @return
     */
    @ApiOperation(value = "ShAddress根据ID删除",notes = "根据ID删除ShAddress方法详情")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "addressId", value = "addressId", required = true, dataType = "Integer")}
    )
    @DeleteMapping(value = "/{addressId}" )
    public CommonResult delete(@PathVariable @NotNull Integer addressId){
        // 根据token，获取Redis中的userId
        Long userId = Integer.toUnsignedLong((Integer) RedisUtil.get(jwtTokenUtil.getToken(request)));

        //调用ShAddressService实现根据主键删除
        addressService.delete(userId, addressId);
        return CommonResult.success();
    }
}
